package com.common.exception;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.common.constant.Codes;
import com.common.base.Return;

/**
 * 异常处理器
 * 
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(CustomException.class)
	public Return handleCustomException(CustomException e){
		return Return.error(e.getCode(),e.getMsg());
	}

	@ExceptionHandler(UnloginException.class)
	public Return handleUnloginException(UnloginException e){
		return Return.error(e.getCode(),e.getMsg());
	}

	@ExceptionHandler(ParamsErrorException.class)
	public Return handleParamsErrorException(ParamsErrorException e){
		return Return.error(e.getCode(),e.getMsg());
	}


		@ExceptionHandler(UnknownAccountException.class)
	public Return handleUnknownAccountException(UnknownAccountException e){
		//账号输入错误
		return Return.error(Codes.LOGIN_REEOR.getCode(),Codes.LOGIN_REEOR.getMessage());
	}

	@ExceptionHandler(AuthorizationException.class)
	public Return handleAuthorizationException(AuthorizationException e){
		//没有访问权限
		return Return.error(Codes.METHOD_NOT_ALLOWED.getCode(),Codes.METHOD_NOT_ALLOWED.getMessage());
	}

	@ExceptionHandler(IncorrectCredentialsException.class)
	public Return handleIncorrectCredentialsException(IncorrectCredentialsException e){
		//账号或密码错误
		return Return.error(Codes.LOGIN_REEOR.getCode(),Codes.LOGIN_REEOR.getMessage());
	}

	@ExceptionHandler(Exception.class)
	public Return handleException(Exception e){
		logger.error("服务器接口异常", e);
		return Return.error();
	}
}